//
//  AdaptSecondCtr.m
//  tableViewDemo
//
//  Created by lix on 16/8/26.
//  Copyright © 2016年 lix. All rights reserved.
//

#import "AdaptSecondCtr.h"
#import "ModelAdapt.h"

@interface AdaptSecondCtr ()


@end

@implementation AdaptSecondCtr

- (id)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"添加元素";
    
    self.view.backgroundColor = [UIColor whiteColor];
    //取消navigation适应对光标造成的影响
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initView];
}

- (void)initView{
    
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 75, WIDTH-20, 200)];
    _textView.delegate = self;
    //字体大小
    _textView.font = [UIFont systemFontOfSize:16];
    //添加滚动区域
    //_textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);
    //不可以滚动
    _textView.scrollEnabled = NO;
    //获得焦点
    [_textView becomeFirstResponder];
    //设置圆角
    [_textView.layer setCornerRadius:10];
    _textView.backgroundColor = GColor;
    //返回键的类型
    _textView.returnKeyType = UIReturnKeyDefault;
    //键盘类型
    _textView.keyboardType = UIKeyboardTypeDefault;

    [self.view addSubview:_textView];
    
    [self setHiddenKeyBoardKey];
    
    //提交按钮
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(125, 300, 125, 40);
    //边框
    button.layer.borderWidth = 0.5;
    button.layer.borderColor = [SubColor CGColor];
    //
    [button setTitle:@"添加" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitleColor:SubColor forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(addText:) forControlEvents:UIControlEventTouchUpInside];
    
    [button.layer setCornerRadius:10];
    [self.view addSubview:button];
}

- (void)addText:(UIButton *)btn{
    //获取最后一个元素的id
    NSArray * fetchResult = [_data selectAll];
    int dataId;
    if (fetchResult != NULL) {
        ModelAdapt * data = [fetchResult objectAtIndex:[fetchResult count]-1];
        dataId = [data.adapt_id intValue];
    }
    else{
        dataId = -1;
    }
    
    _data = [NSEntityDescription insertNewObjectForEntityForName:@"ModelAdapt" inManagedObjectContext:[ModelAdapt sharedCoreDataManager].managedObjContext];
    [_data setAdapt_content:_textView.text];
    [_data setAdapt_id:[NSNumber numberWithInt:dataId+1]];
    
    NSError *error = nil;
    
    //    托管对象准备好后，调用托管对象上下文的save方法将数据写入数据库
    BOOL isSaveSuccess = [[ModelAdapt sharedCoreDataManager].managedObjContext save:&error];
    if (!isSaveSuccess) {
        NSLog(@"Error: %@,%@",error,[error userInfo]);
    }else
    {
        NSLog(@"Save successFull");
    }
    
    
}

//设置键盘隐藏按钮事件
- (void)setHiddenKeyBoardKey{
    //定义一个toolBar
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    //设置style
    [topView setBarStyle:UIBarStyleBlack];
    
    //定义两个flexibleSpace的button，放在toolBar上，这样完成按钮就会在最右边
    UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //定义完成按钮
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];
    
    //在toolBar上加上这些按钮
    NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
    [topView setItems:buttonsArray];
    
    [_textView setInputAccessoryView:topView];
}

- (void)resignKeyboard{
     [_textView resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
